home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0493 / PASCAL-3.FAQ < prev    next >
Text File  |  1993-04-13  |  4KB  |  125 lines

  1. ─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 393 of 439                                                               
  3. From : Max Maischein                       2:249/6.17           09 Apr 93  19:19 
  4. To   : All                                                                       
  5. Subj : FAQ ]I[                                                                
  6. ────────────────────────────────────────────────────────────────────────────────
  7. PASCAL.FAQ             Frequently asked questions about Pascal
  8.  
  9. The aim of this document is to give answers to frequently asked
  10. questions in the pascal echo. Thumb rules before asking for help are to
  11. look in the manuals and in the online help first. Many problems can be
  12. solved by just looking into either / both of them. Here are some
  13. topics, that come very often in the Pascal Echo.
  14.  
  15.                                  Part ]I[
  16.                 #6: Controling user input and the cursor
  17.  
  18. ---------------------------------------------------------------------
  19.                 #6: Controling user input and the cursor
  20. Q1: How to I read a number from the keyboard ?
  21. Q2: How can I use the cursor keys with ReadLn ?
  22.  
  23. A1: The simplest way would be just to code :
  24.  
  25. ReadLn( Number );
  26.  
  27.     Here, you would have to turn off IO-checking, because if the user
  28.     enters a string ( "How old are you ?" - "eighteen" ), and do the
  29.     checking with IOResult.
  30.  
  31. A2: Another way would be to read a string and then do the parsing with
  32. the procedure Val(), any wrong input would result in the error variable
  33. being non-zero.
  34.  
  35. A3: If you need support for cursor down, ESC and other keys, you will
  36. need to do it all by yourself. The procedure below might not be the
  37. fastest, but you can specify all keys that are allowed to be typed, e.g.
  38. for file names, "?" and "*" would not be allowed :
  39.  
  40. Uses CRT;
  41.  
  42. Type TCharSet = Set of Char;
  43. Const Alpha : TCharSet = ['A'..'Z','a'..'z'];
  44.       Numbers : TCharSet = ['0'..'9'];
  45.       HexNumbers : TCharSet = ['0'..'9', 'A'..'F', 'a'..'f'];
  46.  
  47. Function StringOf( Ch : Char; Len : Byte ) : String;
  48. Var Result : String;
  49. Begin
  50.   FillChar( Result[ 1 ], Len, Ch );
  51.   Result[ 0 ] := Char( Len );
  52.   StringOf := Result;
  53. End;
  54.  
  55. Function GetInput( X,Y,MaxLen : Byte; Const Default : String; Allowed :
  56. TCharSet ) : String;
  57. Var Result : String;
  58.     CursorPos : Byte;
  59.     Done : Boolean;
  60.     Ch : Char;
  61. Begin
  62.   Result := Default;
  63.   Done := False;
  64.   CursorPos := Length( Result )+1;
  65.   Repeat
  66.     GotoXY( X,Y );
  67.     Write( StringOf( #176, MaxLen ));
  68.     GotoXY( X,Y );
  69.     Write( Result );
  70.     GotoXY( X+CursorPos-1, Y );
  71.     Ch := ReadKey;
  72.     Case Ch of
  73.        #0 : Case ReadKey of
  74.               'K' : If CursorPos > 1
  75.                       then Dec( CursorPos );
  76.               'M' : If CursorPos < Succ( Length( Result ))
  77.                       then Inc( CursorPos );
  78.             End;
  79.   #127,#8 : If CursorPos > 1
  80.               then
  81.                 Begin
  82.                   Dec( CursorPos );
  83.                   Delete( Result, CursorPos,1 );
  84.                 End;
  85.       #13 : Done := True;
  86.       #27 : Begin
  87.               Result := '';
  88.               Done := True;
  89.             End;
  90.       else
  91.         If ( Ch in Allowed ) and ( Length( Result ) < MaxLen )
  92.           then
  93.             Begin
  94.               Insert( Ch, Result, Cursorpos );
  95.               Inc( CursorPos );
  96.             End;
  97.     End;
  98.   Until Done;
  99.   GetInput := Result;
  100. End;
  101.  
  102. Begin
  103.   WriteLn( GetInput( 10,10,10,'Test',Alpha ));
  104.   WriteLn( GetInput( 10,10,20,'1234',Numbers));
  105.   WriteLn( GetInput( 10,10,20,'1234',HexNumbers ));
  106. End.
  107.  
  108. A4: Switch to Turbo Vision. This the most complicated way of getting a
  109. full size input line, but you get full mouse support and windows with
  110. it. If you don't need either, you can as well use the aboe routine, but
  111. if you plan on going mousewards, you're better off with TV or other
  112. 'Application Frameworks'. The words to look for in the TV-Manuals are
  113. Insert, InsertWindow, GetData / SetData, TInputLine and TWindow.
  114.  
  115. ---------------------------------------------------------------------
  116.  
  117. -max
  118.  
  119. This file is copyrighted by Max Maischein, 2:249/6.17.
  120. No part thereof may be printed without my written permission.
  121.  
  122. ---
  123.  * Origin: Arrays of Pointers to Arrays of Pointers to ... (2:249/6.17)
  124.  
  125.